#include "employeeleaveform.h"
#include "ui_employeeleaveform.h"

EmployeeLeaveForm::EmployeeLeaveForm(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::EmployeeLeaveForm),
    manager(new QNetworkAccessManager(this))
{
    ui->setupUi(this);


    // 获取当前日期
    QDate currentDate = QDate::currentDate();
    ui->dateEdit->setDate(currentDate);
    ui->StartDate->setDate(currentDate);
    QDate nextDay = currentDate.addDays(1);
    ui->EndDate->setDate(nextDay);
}

EmployeeLeaveForm::~EmployeeLeaveForm()
{
    delete ui;
}

void EmployeeLeaveForm::on_pushButton_clicked()
{
    // 1. 获取表单数据
    QString employeeId = ui->lineEdit->text().trimmed();
    QString leaveDate = ui->dateEdit->date().toString("yyyy-MM-dd");
    QString leaveType = ui->comboBox->currentText();

    // 转换请假类型为后端识别格式
    if(leaveType == "早上") {
        leaveType = "morning";
    } else if(leaveType == "下午") {
        leaveType = "afternoon";
    } else if(leaveType == "全天") {
        leaveType = "full";
    }

    // 2. 数据校验
    if (employeeId.isEmpty()) {
        QMessageBox::warning(this, "输入错误", "员工ID不能为空");
        return;
    }
    if (leaveDate.isEmpty()) {
        QMessageBox::warning(this, "输入错误", "请选择请假日期");
        return;
    }

    // 显示加载弹窗
    QProgressDialog *loadingDialog = new QProgressDialog("正在提交请假申请...", "取消", 0, 0, this);
    loadingDialog->setWindowTitle("请稍候");
    loadingDialog->setWindowModality(Qt::WindowModal);
    loadingDialog->show();

    // 3. 构建请求参数
    QJsonObject params; // 使用 QJsonObject 而不是 QMap
    params["employeeId"] = employeeId; // 注意：这里的键是 "employeeId"，和服务器端 params["employeeId"] 对应
    params["recordDate"] = leaveDate;
    params["leaveType"] = leaveType;

    // 4. 发送POST请求
    HttpManager::instance()->postJson(
                QString(SERVER_BASE_URL) + "/api/attend/leave",
                params,
                [this, loadingDialog](const QJsonObject& result) {
        // 处理成功响应
        loadingDialog->close();
        delete loadingDialog;

        if (result["success"].toBool()) {
            QMessageBox::information(this, "成功", "请假申请提交成功");
        } else {
            QMessageBox::warning(this, "失败", result["error"].toString());
        }
    },
    [this, loadingDialog](int code, const QString& msg) {
        // 处理请求错误
        loadingDialog->close();
        delete loadingDialog;

        QMessageBox::critical(this, "网络错误",
                              QString("请求失败：错误码 %1，信息：%2").arg(code).arg(msg));
    }
    );
}

void EmployeeLeaveForm::on_pushButton_3_clicked()
{
    // 1. 获取表单数据
    QString employeeId = ui->lineEdit->text().trimmed();
    QString startDate = ui->StartDate->date().toString("yyyy-MM-dd");
    QString startTimeType = ui->StartTimeType->currentText();
    QString endDate = ui->EndDate->date().toString("yyyy-MM-dd"); // 修正：应使用 EndDate
    QString endTimeType = ui->EndTimeType->currentText();       // 修正：应使用 EndTimeType

    // 转换时间段类型为后端识别格式
    if (startTimeType == "早上") {
        startTimeType = "morning";
    } else if (startTimeType == "下午") {
        startTimeType = "afternoon";
    }

    if (endTimeType == "早上") {
        endTimeType = "morning";
    } else if (endTimeType == "下午") {
        endTimeType = "afternoon";
    }

    // 2. 数据校验
    if (employeeId.isEmpty()) {
        QMessageBox::warning(this, "输入错误", "员工ID不能为空");
        return;
    }

    QDate start = QDate::fromString(startDate, "yyyy-MM-dd");
    QDate end = QDate::fromString(endDate, "yyyy-MM-dd");

    if (!start.isValid() || !end.isValid()) {
        QMessageBox::warning(this, "输入错误", "请选择有效的开始和结束日期");
        return;
    }

    if (start > end) {
        QMessageBox::warning(this, "输入错误", "开始日期不能晚于结束日期");
        return;
    }


    // 显示加载弹窗
    QProgressDialog *loadingDialog = new QProgressDialog("正在提交请假申请...", "取消", 0, 0, this);
    loadingDialog->setWindowTitle("请稍候");
    loadingDialog->setWindowModality(Qt::WindowModal);
    loadingDialog->show();

    // 3. 构建请求参数 (与后端 handleLeaveByTimeRange 接口对应)
    QJsonObject params;
    params["employeeId"] = employeeId;
    params["startDate"] = startDate;
    params["endDate"] = endDate;
    params["startTimeType"] = startTimeType;
    params["endTimeType"] = endTimeType;

    // 4. 发送POST请求
    HttpManager::instance()->postJson(
                QString(SERVER_BASE_URL) + "/api/attend/leaveByTimeRange", // 注意：URL是 leaveByTimeRange
                params,
                [this, loadingDialog](const QJsonObject& result) {
        // 处理成功响应
        loadingDialog->close();
        delete loadingDialog;

        if (result["success"].toBool()) {
            QMessageBox::information(this, "成功", "请假申请提交成功");
        } else {
            QMessageBox::warning(this, "失败", result["error"].toString());
        }
    },
    [this, loadingDialog](int code, const QString& msg) {
        // 处理请求错误
        loadingDialog->close();
        delete loadingDialog;

        QMessageBox::critical(this, "网络错误",
                              QString("请求失败：错误码 %1，信息：%2").arg(code).arg(msg));
    }
    );
}

void EmployeeLeaveForm::on_StartDate_dateTimeChanged(const QDateTime &dateTime)
{
    QDate start = ui->StartDate->date();
    QDate end = ui->EndDate->date();

    if (start >= end) {
        QDate nextDay = start.addDays(1);
        ui->EndDate->setDate(nextDay);
    }
}



void EmployeeLeaveForm::on_EndDate_dateTimeChanged(const QDateTime &dateTime)
{
    QDate start = ui->StartDate->date();
    QDate end = ui->EndDate->date();

    if (start >= end) { // 使用 >= 运算符
        QMessageBox::warning(this, "输入错误", "开始日期必须早于结束日期，且不能是同一天");
        QDate nextDay = start.addDays(1);
        ui->EndDate->setDate(nextDay);
        return;
    }
}
